﻿using HslCommunication;
using Newtonsoft.Json.Linq;
using SYSoft.Comm;
using SYSoft.Comm.Utils;
using SYSoft.Model.Models.DeviceManage;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace SYSoft
{
    public class ServerInfo
    {


        /// <summary>
        /// 油漆线产量
        /// </summary>
        public static int YouQiChanLiang = 0;



        //配置信息
        /// <summary>
        /// 运行状态  电流标准
        /// </summary>
        public static float RunStatusCurrent = 5;

        /// <summary>
        /// 电能标准值
        /// </summary>
        public static float EnnrgyStandard = 2000;
        /// <summary>
        /// 天然气消耗标准值
        /// </summary>
        public static float GasnnrgyStandard = 2000;

        /// <summary>
        /// 单节标准能耗
        /// </summary>
       public static float UnitEnnrgyStandard = 220.7f;

        /// <summary>
        /// 单节标准能耗  ["等离子", "埋弧焊", "预拱","自动焊", "剪折线", "机器人","抛丸机", "油漆线", "组焊","翻转机"],
        /// </summary>
        public static float[] AllUnitEnnrgyStandard = new float[10] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
        /// <summary>
        /// AR气设定量
        /// </summary>
        public static float ARSetting = 30000;
        /// <summary>
        /// CO2气设定量
        /// </summary>
        public static float CO2Setting = 500;
        /// <summary>
        /// 天然气设定量
        /// </summary>
        public static float GasSetting = 30000;


        /// <summary>
        /// 按IP作为key值  一个IP开启一个线程
        /// 站点 作为int  key
        /// </summary>
        public static Dictionary<int, List<DevicePassConfig>> devicePasses = new Dictionary<int, List<DevicePassConfig>>();


        /// <summary>
        /// 设备  电流配置
        /// </summary>
        public static Dictionary<int, DeviceParamSetting> deviceParamSetings = new Dictionary<int, DeviceParamSetting>();


        /// <summary>
        /// 按IP作为key值  一个IP开启一个线程
        /// 站点 作为int  key
        /// </summary>
        public static Dictionary<int, List<DeviceSwitchConfig>> deviceSwitchs = new Dictionary<int, List<DeviceSwitchConfig>>();



        /// <summary>
        /// 油漆密度
        /// </summary>
        public static float YouqiMidu = 1.58f;

        /// <summary>
        /// 固化剂密度
        /// </summary>
        public static float GuhuajiMidu = 1.12f;

        /// <summary>
        /// 加载开关模块配置
        /// </summary>
        /// <returns></returns>
        public static OperateResult loadSwitchConfig()
        {
            MysqlHelper mysql = new MysqlHelper();
            string sql = "select * from device_switch_config";
            var dt = mysql.ExecuteQuery(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (deviceSwitchs.ContainsKey(Convert.ToInt32(dt.Rows[i]["DeviceId"])))
                    {
                        deviceSwitchs[Convert.ToInt32(dt.Rows[i]["DeviceId"])].Add(new DeviceSwitchConfig
                        {
                            DeviceId = Convert.ToInt32(dt.Rows[i]["DeviceId"]),
                            DeviceName = dt.Rows[i]["DeviceName"].ToString(),
                            PassIndex = Convert.ToInt32(dt.Rows[i]["PassIndex"]),
                            PassName = dt.Rows[i]["PassName"].ToString(),
                            IpAddress = dt.Rows[i]["IpAddress"].ToString(),

                        });
                    }
                    else
                    {
                        deviceSwitchs.Add(Convert.ToInt32(dt.Rows[i]["DeviceId"]), new List<DeviceSwitchConfig>());
                        deviceSwitchs[Convert.ToInt32(dt.Rows[i]["DeviceId"])].Add(new DeviceSwitchConfig
                        {
                            DeviceId = Convert.ToInt32(dt.Rows[i]["DeviceId"]),
                            DeviceName = dt.Rows[i]["DeviceName"].ToString(),
                            PassIndex = Convert.ToInt32(dt.Rows[i]["PassIndex"]),
                            PassName = dt.Rows[i]["PassName"].ToString(),
                            IpAddress = dt.Rows[i]["IpAddress"].ToString(),

                        });
                    }
                }
                return OperateResult.CreateSuccessResult();
            }
            else
            {
                return new OperateResult("未查询到数据");
            }



           
        }

        public static OperateResult LoadPu3000Config()
        {
            MysqlHelper mysql = new MysqlHelper();
            string sqlt = "select * from pu3000config";
            var dtt = mysql.ExecuteQuery(sqlt);
            if (dtt != null && dtt.Rows.Count > 0)
            {
                for (int i = 0; i < dtt.Rows.Count; i++)
                {
                    if (dtt.Rows[i]["PlcAddress"].ToString() == "Config")
                    {
                        YouqiMidu = Convert.ToSingle(dtt.Rows[i]["Param1"]);
                        GuhuajiMidu = Convert.ToSingle(dtt.Rows[i]["Param3"]);
                    }
                }
                return OperateResult.CreateSuccessResult();
            }
            else
            {
                return new OperateResult("未查询到数据");
            }
        }

         public static OperateResult saveConfig()
        {
            JObject obj = new JObject();
            obj.Add("ARSetting", ARSetting);
            obj.Add("RunStatusCurrent", RunStatusCurrent);
            obj.Add("CO2Setting", CO2Setting);
            obj.Add("GasSetting", GasSetting);
            obj.Add("EnnrgyStandard", EnnrgyStandard);
            obj.Add("GasnnrgyStandard", GasnnrgyStandard);
            obj.Add("UnitEnnrgyStandard", UnitEnnrgyStandard);
            obj.Add("AllUnitEnnrgyStandard", new JArray(AllUnitEnnrgyStandard));
           return NetFactory.redisPool.WriteKey("MTCD:SystemConfig", obj.ToString());
        }

        /// <summary>
        /// 加载系统各种配置
        /// </summary>
        /// <returns></returns>
        public static OperateResult loadConfig()
        {
            var res = NetFactory.redisPool.ReadKey("MTCD:SystemConfig");
            if (!res.IsSuccess)
            {
                return res;
            }
            try
            {
                JObject obj = JObject.Parse(res.Content);
                ARSetting = obj.Value<float>("ARSetting");
                RunStatusCurrent = obj.Value<float>("RunStatusCurrent");
                CO2Setting = obj.Value<float>("CO2Setting");
                GasSetting = obj.Value<float>("GasSetting");
                EnnrgyStandard = obj.Value<float>("EnnrgyStandard");
                GasnnrgyStandard = obj.Value<float>("GasnnrgyStandard");
                UnitEnnrgyStandard = obj.Value<float>("UnitEnnrgyStandard");
                JArray array1 = JArray.Parse(obj["AllUnitEnnrgyStandard"].ToString());
                float[] alarm = array1.ToObject<float[]>();
                for (int i = 0; i < alarm.Length; i++)
                {
                    AllUnitEnnrgyStandard[i] = alarm[i];
                }
                return OperateResult.CreateSuccessResult();
            }
            catch (Exception ex)
            {
                return new OperateResult(ex.Message);
            }
           


        }



    

        public static OperateResult loadPass()
        {
            MysqlHelper mysql = new MysqlHelper();
            string sql = "select * from device_pass_config";
            var dt = mysql.ExecuteQuery(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (devicePasses.ContainsKey(Convert.ToInt32(dt.Rows[i]["DeviceId"])))
                    {
                        devicePasses[Convert.ToInt32(dt.Rows[i]["DeviceId"])].Add(new DevicePassConfig
                        {
                            DeviceId = Convert.ToInt32(dt.Rows[i]["DeviceId"]),
                            DeviceName = dt.Rows[i]["DeviceName"].ToString(),
                            PassIndex = Convert.ToInt32(dt.Rows[i]["PassIndex"]),
                            PassName = dt.Rows[i]["PassName"].ToString(),
                            PassUnit = dt.Rows[i]["PassUnit"].ToString(),
                            PassMultiple = Convert.ToSingle(dt.Rows[i]["PassMultiple"]),
                            IpAddress = dt.Rows[i]["IpAddress"].ToString(),
                            StationId = Convert.ToInt32(dt.Rows[i]["StationId"]),
                            upLimitData = Convert.ToSingle(dt.Rows[i]["upLimitData"]),
                            lowLimitData = Convert.ToSingle(dt.Rows[i]["lowLimitData"]),
                            midLimitData = Convert.ToSingle(dt.Rows[i]["midLimitData"]),

                            wh_lowLimitData = dt.Rows[i]["wh_lowLimitData"] == DBNull.Value ? 0 : Convert.ToSingle(dt.Rows[i]["wh_lowLimitData"]),
                            wh_upLimitData = dt.Rows[i]["wh_upLimitData"] == DBNull.Value ? 0 : Convert.ToSingle(dt.Rows[i]["wh_upLimitData"]),
                            wh_Text1 = dt.Rows[i]["wh_Text1"].ToString(),
                            wh_Text2 = dt.Rows[i]["wh_Text2"].ToString()
                        });
                    }
                    else
                    {
                        devicePasses.Add(Convert.ToInt32(dt.Rows[i]["DeviceId"]), new List<DevicePassConfig>());
                        devicePasses[Convert.ToInt32(dt.Rows[i]["DeviceId"])].Add(new DevicePassConfig
                        {
                            DeviceId = Convert.ToInt32(dt.Rows[i]["DeviceId"]),
                            DeviceName = dt.Rows[i]["DeviceName"].ToString(),
                            PassIndex = Convert.ToInt32(dt.Rows[i]["PassIndex"]),
                            PassName = dt.Rows[i]["PassName"].ToString(),
                            PassUnit = dt.Rows[i]["PassUnit"].ToString(),
                            PassMultiple = Convert.ToSingle(dt.Rows[i]["PassMultiple"]),
                            IpAddress = dt.Rows[i]["IpAddress"].ToString(),
                            StationId = Convert.ToInt32(dt.Rows[i]["StationId"]),
                            upLimitData = Convert.ToSingle(dt.Rows[i]["upLimitData"]),
                            lowLimitData = Convert.ToSingle(dt.Rows[i]["lowLimitData"]),
                            midLimitData = Convert.ToSingle(dt.Rows[i]["midLimitData"]),

                            wh_lowLimitData = dt.Rows[i]["wh_lowLimitData"]==DBNull.Value?0: Convert.ToSingle(dt.Rows[i]["wh_lowLimitData"]),
                            wh_upLimitData = dt.Rows[i]["wh_upLimitData"] == DBNull.Value ? 0 : Convert.ToSingle(dt.Rows[i]["wh_upLimitData"]),
                            wh_Text1 = dt.Rows[i]["wh_Text1"].ToString(),
                            wh_Text2 = dt.Rows[i]["wh_Text2"].ToString()
                        });
                    }
                }
                return OperateResult.CreateSuccessResult();
            }
            else
            {
                return new OperateResult("未查询到数据");
            }
        }


        /// <summary>
        /// 加载系统 电流参数
        /// </summary>
        /// <returns></returns>
        public static OperateResult loadParamSettins()
        {
            MysqlHelper mysql = new MysqlHelper();
            string sql = "select * from device_param_set";
            var dt = mysql.ExecuteQuery(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    deviceParamSetings.Add(Convert.ToInt32(dt.Rows[i]["DeviceId"]), new DeviceParamSetting() 
                    { 
                        StatusCurrent = Convert.ToInt32(dt.Rows[i]["StatusCurrent"]),
                        RunCurrent = Convert.ToInt32(dt.Rows[i]["RunCurrent"]),
                        LoadCurrent = Convert.ToInt32(dt.Rows[i]["LoadCurrent"])
                    });
                }
                return OperateResult.CreateSuccessResult();
            }
            else
            {
                return new OperateResult("未查询到数据");
            }
        }

        public static bool CheckArray(bool[] data1,bool[] data2)
        {
            for (int i = 0; i < data1.Length; i++)
            {
                if (data1[i] != data2[i])
                    return true;
            }
            return false;
        }



        public static string GetDevicename(int deviceid)
        {
            switch (deviceid)
            {
                case 1001: return "等离子";
                case 1002: return "埋弧焊";
                case 1003: return "剪折线";
                case 1004: return "预拱;";
                case 1005: return "自动焊";
                case 1006: return "组焊; ";
                case 1007: return "抛丸机";
                case 1008: return "油漆线";
                case 1009: return "南跨20T行车";
                case 1010: return "北跨20T行车";
                case 1011: return "等离子行车";
                case 1012: return "剪折半门吊";
                case 1013: return "组焊半门吊";
                case 1014: return "机器人半门吊";
                case 1015: return "抛丸机上线行车";
                case 1016: return "油漆上线";
                case 1017: return "高配房";
                case 1018: return "空压机";
                case 1019: return "混合气站";
                case 1020: return "天然气站";
                case 1021: return "机器人";
                case 1022: return "PU3000";
                case 1023: return "安调";
                case 10061: return "翻转机";
            }
            return "";
        }


    }
}
